package com.fengyun.scala

/**
 * @author Hanpeng
 * @date 2021/1/29 17:27
 * @description:  有两个参数的function.
 */
object TestFunction2 {
  def main(args: Array[String]): Unit = {
    // (Int, Int) => Int  入参有两个Int  出参有一个 类型为Int
    // =
    // (x: Int, y: Int) => if (x < y) y else x  有两个参数
    val max: (Int, Int) => Int = (x: Int, y: Int) => if (x < y) y else x;
    //等价于  去掉入参和出参的定义
    val max1 = (x: Int, y: Int) => if (x < y) y else x;
    // anonfun2 和  anonfun3是等价的 anonfun2 是简写
    val anonfun2 = new ((Int, Int) => Int) {
      def apply(x: Int, y: Int): Int = if (x < y) y else x
    }
    val anonfun3 = new Function2[Int, Int, Int] {
      def apply(x: Int, y: Int): Int = if (x < y) y else x
    }

    val anonfun4 = new Function2[Int, Int, Double] {
      def apply(x: Int, y: Int): Double = {123.23}
    }
    val anonfun5: ((Int, Int) => Double) = new Function2[Int, Int, Double] {
      def apply(x: Int, y: Int): Double = {
        123.23
      }
    }
    println(anonfun5(0,1)) //最后方法由 apply 执行
    // curried  创建当前函数的柯里化版本
    val curried: Int => Int => Double = anonfun5.curried
    println(curried(1)(1))
    println("===============")
    //tupled 创建函数的一个元组的版本
    val tupled: ((Int, Int)) => Double = anonfun5.tupled

    println(tupled((1,2)))




  }
}
